package com.um.jdy.platform.market.manager;

import com.um.jdy.common.market.entity.dto.ActivityDTO;
import com.um.jdy.common.market.entity.dto.FullReduceDTO;
import com.um.jdy.common.market.entity.po.Activity;
import com.um.jdy.common.market.entity.po.FullReduce;
import com.um.jdy.common.market.entity.po.FullReduceGoods;
import com.um.jdy.common.market.entity.vo.ActivityVO;
import com.um.jdy.common.market.entity.vo.FullReduceVO;
import com.um.jdy.common.market.service.ActivityService;
import com.um.jdy.common.market.service.FullReduceGoodsService;
import com.um.jdy.common.market.service.FullReduceService;
import com.um.jdy.common.shop.entity.po.Goods;
import com.um.jdy.common.shop.entity.po.GoodsOption;
import com.um.jdy.common.shop.entity.vo.GoodsVO;
import com.um.jdy.common.shop.service.GoodsOptionService;
import com.um.jdy.common.shop.service.GoodsService;
import com.um.jdy.platform.market.entity.dto.ActivityPageDTO;
import com.um.jdy.platform.market.entity.dto.SaveReduceDTO;
import com.um.jdy.platform.market.entity.vo.ActivityGoodsResultVO;
import com.um.jdy.platform.market.entity.vo.FullReduceGoodsResultVO;
import com.um.jdy.platform.market.entity.vo.FullReduceItemVO;
import com.um.jdy.platform.market.entity.vo.FullReduceResultVO;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.manager.BasePlusManager;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 管理类：满减活动表
 */
@Component
@Transactional
public class FullReduceManager extends BasePlusManager<FullReduceService, FullReduce, FullReduceDTO, FullReduceVO, ActivityPageDTO, ListDTO> {

    @Autowired
    FullReduceGoodsService fullReduceGoodsService;

    @Autowired
    ActivityService activityService;

    @Autowired
    ActivityManager activityManager;

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsOptionService goodsOptionService;

    public boolean saveReduce(SaveReduceDTO saveReduceDTO) {

        ActivityDTO activityDTO = saveReduceDTO.getActivity();

        List<ActivityGoodsResultVO> goodsList = saveReduceDTO.getGoodsList();

        List<FullReduceDTO> fullReduceList = saveReduceDTO.getFullReduceList();

        String marketActivityId = StringUtil.getTrim(activityDTO.getMarketActivityId());

        if (marketActivityId.isEmpty()) {
            activityDTO.setCreateTime(new Date());
        }

        Activity activity = UMBeanUtils.copyBean(activityDTO, Activity.class);

        activityService.save(activity);

        marketActivityId = StringUtil.getTrim(activity.getMarketActivityId());

        service.delete("market_activity_id", marketActivityId);
        fullReduceGoodsService.delete("market_activity_id", marketActivityId);

        for (FullReduceDTO reduceDTO : fullReduceList) {
            reduceDTO.setMarketFullReduceId(null);
            reduceDTO.setMarketActivityId(marketActivityId);
            FullReduce fullReduce = UMBeanUtils.copyBean(reduceDTO, FullReduce.class);
            service.save(fullReduce);
        }

        for (ActivityGoodsResultVO goods : goodsList) {
            String goodsId = StringUtil.getTrim(goods.getShopGoodsId());
            if (activityManager.validateGoodsInActivity(goodsId, marketActivityId)) {
                Goods entity = goodsService.getEntity(goodsId);
                throw new ServiceException("保存失败，" + StringUtil.getTrim(entity.getShopGoodsName()) + "已参加活动！");
            }
            FullReduceGoods fullReduceGoods = new FullReduceGoods();
            fullReduceGoods.setShopGoodsId(goodsId);
            fullReduceGoods.setMarketActivityId(marketActivityId);
            fullReduceGoodsService.save(fullReduceGoods);
        }

        return true;

    }

    public FullReduceResultVO getReduce(FullReduceDTO reduceDTO) {
        String marketActivityId = StringUtil.getTrim(reduceDTO.getMarketActivityId());

        Activity activity = activityService.getEntity(marketActivityId);

        List<FullReduce> fullReduceList = service.getList("market_activity_id", marketActivityId, "type", "asc");

        List<FullReduceItemVO> fullReduceItemVOList = UMBeanUtils.copyBeanList(fullReduceList, FullReduceItemVO.class);

        List<FullReduceGoodsResultVO> goodsList = service.getViewModelList("get_full_reduce_goods_list", FullReduceGoodsResultVO.class, new String[]{marketActivityId,getComId()});
        for (FullReduceGoodsResultVO goods : goodsList) {
            goods.setThumbList(JSONUtil.parseJSONToList(StringUtil.getTrim(goods.getThumb())));
        }

        for (FullReduceItemVO reduce : fullReduceItemVOList) {
            String goodsId = StringUtil.getTrim(reduce.getShopGoodsId());

            Goods goods = goodsService.getEntity(goodsId);
            if (!goodsId.isEmpty() && goods != null) {

                GoodsVO goodsVO = new GoodsVO();
                goodsVO.setShopGoodsId(goodsId);
                goodsVO.setShopGoodsName(goods.getShopGoodsName());
                goodsVO.setThumb(goods.getThumb());
                goodsVO.setGoodsCode(goods.getGoodsCode());
                reduce.setGoodsObj(goodsVO);

                GoodsOption goodsOption = goodsOptionService.getEntity(reduce.getShopGoodsOptionId());

                if (goodsOption != null) {
                    goodsVO.setOptions(goodsOption.getTitle());
                }
            }
        }

        FullReduceResultVO resultVO = new FullReduceResultVO();
        resultVO.setActivity(UMBeanUtils.copyBean(activity, ActivityVO.class));
        resultVO.setFullReduceList(fullReduceItemVOList);
        resultVO.setGoodsList(goodsList);
        return resultVO;

    }

}
